package com.zz.yt.lib.bluetooth.delegate.print.zk;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.whf.android.jar.util.log.LatteLogger;
import com.whf.android.jar.util.storage.LattePreference;
import com.zz.yt.lib.bluetooth.R;
import com.zz.yt.lib.bluetooth.base.BaseBluetoothDelegate;
import com.zz.yt.lib.bluetooth.base.Bluetooth;
import com.zz.yt.lib.bluetooth.bt.printer.zpBluetoothPrinter;
import com.zz.yt.lib.bluetooth.delegate.print.zk.modle.PrintZkManager;
import com.zz.yt.lib.ui.CustomTitleBar;



/**
 * 芝柯蓝牙打印
 *
 * @author qf
 * @version 1.0
 **/
public class PrintDelegate extends BaseBluetoothDelegate {

    protected zpBluetoothPrinter zpSDK = null;

    @NonNull
    public static PrintDelegate create() {
        Bundle args = new Bundle();
        PrintDelegate fragment = new PrintDelegate();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void setCustomTitleBar(@NonNull CustomTitleBar titleBar) {
        titleBar.setText("芝柯蓝牙打印");
    }

    @Override
    protected Object setLayout() {
        return R.layout.blu_delegate_added_print;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        super.onBindView(savedInstanceState, rootView);


        rootView.findViewById(R.id.layout_bt_un).setOnClickListener(v -> onReplace());
        rootView.findViewById(R.id.id_text_replace).setOnClickListener(v -> onReplace());
        rootView.findViewById(R.id.id_btn_print_start).setOnClickListener(v -> startPrint());

    }

    /**
     * 是否连接
     */
    protected void inMac() {
        super.inMac();
        RelativeLayout layoutBtOn = mRootView.findViewById(R.id.layout_bt_on);
        LinearLayout layoutBtUn = mRootView.findViewById(R.id.layout_bt_un);
        /*
         * 显示连接设备
         */
        TextView mTextBtEquipment = mRootView.findViewById(R.id.id_text_bt_equipment);
        if (TextUtils.isEmpty(mac)) {
            //一次都没连接过
            layoutBtOn.setVisibility(View.GONE);
            layoutBtUn.setVisibility(View.VISIBLE);
        } else {
            LatteLogger.i(mac);
            //
            layoutBtOn.setVisibility(View.VISIBLE);
            layoutBtUn.setVisibility(View.GONE);
            //显示连接的蓝牙名称
            final String device = LattePreference.getCustomAppProfile(Bluetooth.BLUETOOTH);
            mTextBtEquipment.setText(device);
        }
    }

    @Override
    protected void inRx() {
        //连接成功
        final String device = LattePreference.getCustomAppProfile(Bluetooth.BLUETOOTH);
        ToastUtils.showShort(device + "连接成功");
    }

    protected void startPrint() {
        if (!isPrint) {
            ToastUtils.showShort("请连接蓝牙");
            return;
        }
        if (zpSDK == null) {
            zpSDK = new zpBluetoothPrinter(context);
        }

        if (!zpSDK.getConnect(mSocket)) {
            hideLoading();
            ToastUtils.showShort("连接失败------(如果多次连接失败，请重启设备，并确保设备在可见范围②)");
            return;
        }
        //设置打印纸张大小
        zpSDK.pageSetup(550, 1100);
        PrintZkManager.create(zpSDK).startLabel();
        print(zpSDK);

    }

    /**
     * @param zpSDK:开始打印
     */
    private void print(@NonNull zpBluetoothPrinter zpSDK) {
        zpSDK.print(0, 1);
        zpSDK.printerStatus();
        int a = zpSDK.GetStatus();
        if (a == -1) {
            //"获取状态异常------";
            hideLoading();
            ToastUtils.showShort("获取状态异常-----");
        }
        if (a == 1) {
            //"缺纸----------";
            hideLoading();
            ToastUtils.showShort("缺纸----------");
        }
        if (a == 2) {
            //"开盖----------";
            hideLoading();
            ToastUtils.showShort("请关闭打印机盖----------");
        }
        if (a == 0) {
            //"打印机正常-------";
            hideLoading();
            ToastUtils.showShort("打印机正常----------");

        }
        zpSDK.disconnect();
        disconnect();
    }

}
